package com.hzit.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentVo;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentServiceImpl implements StudentService {
	//1. 查询学生
	@Override
	public List<Student> findAll() throws IOException {
		//1.1 得到sqlSessionFactory对象
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//1.2 得到SqlSession对象
		try(SqlSession session = sqlSessionFactory.openSession()) {
			//1.3 得到mapper对象
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//1.4 查询所有学生
			List<Student> students = mapper.findAll();
			//1.5 为学生对应的班级名称赋值
			for (Student student : students) {
				Classes c = classesMapper.findById(student.getCid());
				student.setCname(c.getCname());
			}
			//1.6 返回学生列表
			return students;
		}
	}
	//2. 分页查询
	@Override
	public PageResult<Student> findByPage(int page, int pageSize) throws IOException {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 开始查询所有记录
		List<Student> students = this.findAll();
		//2.3 将上面的集合转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.4 构造PageResult对象并返回
		return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
	}

	//3. 分页带条件查询对象
	@Override
	public PageResult<Student> search(int page, int pageSize, StudentVo studentVo) throws IOException {
		//3.1 开始分页查询
		PageHelper.startPage(page,pageSize);
		//3.2 开始条件查询
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		try(SqlSession session = sqlSessionFactory.openSession()){
			StudentMapper studentMapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			List<Student> students = studentMapper.search(studentVo);
			//3.3 为学生对应的班级名称赋值
			for (Student student : students) {
				Classes c = classesMapper.findById(student.getCid());
				student.setCname(c.getCname());
			}
			//3.4 将上面的集合转换为Page<Student>对象
			Page<Student> studentPage = (Page<Student>) students;
			//3.5 构造PageResult对象并返回
			return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
		}

	}

	//4. 添加学生
	@Override
	public void add(Student student) throws IOException {
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.add(student);
		}
	}

	//5. 修改学生
	@Override
	public void update(Student student) throws IOException {
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.update(student);
		}
	}

	//6. 删除学生
	@Override
	public void delete(String sid) throws IOException {
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.delete(sid);
		}
	}
}
